package cn.org.july.weixin.service.template;

import cn.org.july.weixin.common.bean.template.BusinessBean;
import cn.org.july.weixin.common.bean.template.BusinessEnum;
import cn.org.july.weixin.common.bean.template.TemplateMessage;
import cn.org.july.weixin.common.constant.WxConstant;
import cn.org.july.weixin.common.utils.HttpClientUtils;
import cn.org.july.weixin.service.accesstoken.AccessTokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 公众号模版服务
 * Created with IntelliJ IDEA.
 * User:  wanghongjie
 * Date:  2020/9/10 - 06:23
 * <p>
 * Description:
 *
 * @blame Android Team
 */
@Service
@Slf4j
public class TemplateService {

    private final AccessTokenService accessTokenService;

    public TemplateService(AccessTokenService accessTokenService) {
        this.accessTokenService = accessTokenService;
    }

    /**
     * 设置所属行业
     *
     * @return
     */
    public String setIndustry(BusinessEnum one, BusinessEnum two) throws IOException {
        String json = BusinessBean.of(one, two).toJson();
        log.info("set Industry request param is {}", json);
        String results = HttpClientUtils.post(accessTokenService.accessTokenUrl(WxConstant.SET_INDUSTRY), json);
        log.info("set Industry request results is {}", results);
        return results;
    }

    /**
     * 设置所属行业
     *
     * @return
     */
    public String setIndustry(String one, String two) throws IOException {
        String json = BusinessBean.of(one, two).toJson();
        log.info("set Industry request param is {}", json);
        String results = HttpClientUtils.post(accessTokenService.accessTokenUrl(WxConstant.SET_INDUSTRY), json);
        log.info("set Industry request results is {}", results);
        return results;
    }

    /**
     * 设置所属行业
     *
     * @param body 请求对象
     * @return
     * @throws IOException
     */
    public String setIndustry(String body) throws IOException {
        log.info("set Industry request param is {}", body);
        String results = HttpClientUtils.post(accessTokenService.accessTokenUrl(WxConstant.SET_INDUSTRY), body);
        log.info("set Industry request results is {}", results);
        return results;
    }

    /**
     * 获取所属行业信息
     *
     * @return 行业信息
     * @throws IOException
     */
    public String getIndustry() throws IOException {
        String results = HttpClientUtils.get(accessTokenService.accessTokenUrl(WxConstant.GET_INDUSTRY));
        log.info("get Industry request results is {}", results);
        return results;
    }

    /**
     * 获取全部消息模版信息
     *
     * @return
     * @throws IOException
     */
    public String getAllPrivateTemplate() throws IOException {
        String results = HttpClientUtils.get(accessTokenService.accessTokenUrl(WxConstant.GET_ALL_PRIVATE_TEMPLATE));
        log.info("get getAllPrivateTemplate request results is {}", results);
        return results;
    }

    /**
     * 发送模版消息
     *
     * @param message 模版消息对象
     * @return
     * @throws IOException
     */
    public String sendTemplate(TemplateMessage message) throws IOException {
        log.info("sendTemplate message request param is {}", message.toJson());
        String results = HttpClientUtils.post(accessTokenService.accessTokenUrl(WxConstant.TEMPLATE_SEND), message.toJson());
        log.info("sendTemplate request results is {}", results);
        return results;
    }

    /**
     * 发送模版消息
     *
     * @param message 模版消息
     * @return
     * @throws IOException
     */
    public String sendTemplate(String message) throws IOException {
        log.info("sendTemplate message request param is {}", message);
        String results = HttpClientUtils.post(accessTokenService.accessTokenUrl(WxConstant.TEMPLATE_SEND), message);
        log.info("sendTemplate request results is {}", results);
        return results;
    }
}